package com.example.demo.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.Dialect;
import com.example.demo.entity.Person;
import com.example.demo.entity.Qwa;
import com.example.demo.mapper.DialectMapper;
import com.example.demo.mapper.PersonMapper;
import com.example.demo.mapper.QwaMapper;
import com.example.demo.service.IDialectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.utils.Result;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author king
 * @since 2024-05-23
 */
@Service
public class DialectServiceImpl extends ServiceImpl<DialectMapper, Dialect> implements IDialectService {

    @Autowired
    private PersonMapper personMapper;
    @Autowired
    private DialectMapper dialectMapper;
    @Autowired
    private QwaMapper qwaMapper;

    @Override
    public Result add1(String params) {
        Result result=new Result<>();
        JSONObject jsonObject= JSONObject.parseObject(params);
        String title="";//声明对话名
        String username="";//声明用户名
        String flagnum="";//声明对话顺序
        JSONArray qwas=null;
        String module="";
        if(jsonObject.containsKey("title"))
        {
            title=jsonObject.getString("title");
        }
        else
        {
            result.setMessage("未传入对话名");
            return result;
        }
        if(jsonObject.containsKey("username"))
        {
            LambdaQueryWrapper<Person>personLambdaQueryWrapper=new LambdaQueryWrapper<>();
            personLambdaQueryWrapper.eq(Person::getUserName,jsonObject.getString("username"));
            Person person=personMapper.selectOne(personLambdaQueryWrapper);
            if(person==null)
            {
                result.setMessage("未找到该用户");
                return result;
            }
            username=jsonObject.getString("username");
        }
        else
        {
            result.setMessage("未传入用户名");
            return result;
        }
        if(jsonObject.containsKey("flagnum"))
        {
            flagnum=jsonObject.getString("flagnum");
        }
        else
        {
            result.setMessage("未传入对话顺序");
            return result;
        }
        if(jsonObject.containsKey("module"))
        {
            module=jsonObject.getString("module");
        }
        if(jsonObject.containsKey("data"))
        {
             qwas = jsonObject.getJSONArray("data");
        }
        else
        {
            result.setMessage("未传入具体问答对");
            return result;
        }
        QueryWrapper<Dialect> dialectLambdaQueryWrapper12=new QueryWrapper<>();
        dialectLambdaQueryWrapper12.eq("title",title)
                .eq("username",username)
                .eq("flagnum",flagnum);
        Dialect dialect3=dialectMapper.selectOne(dialectLambdaQueryWrapper12);
        if(dialect3!=null)
        {
            result.setMessage(dialect3+"已存在，请重新设置title+username+flagnum");
            return result;
        }
        Dialect dialect=new Dialect();
        dialect.setTitle(title);
        dialect.setUsername(username);
        dialect.setFlagnum(flagnum);
        dialect.setModule(module);
        int p=dialectMapper.insert(dialect);
        if(p!=1)
        {
            result.setMessage("当前对话记录未插入");
            return result;
        }
        else
        {
            QueryWrapper<Dialect> dialectLambdaQueryWrapper1=new QueryWrapper<>();
            dialectLambdaQueryWrapper1.eq("title",title)
           .eq("username",username)
            .eq("flagnum",flagnum);
            Dialect dialect1=dialectMapper.selectOne(dialectLambdaQueryWrapper1);
            if(dialect1==null)
            {
                result.setMessage("当前对话记录在表中未找到");
                return result;
            }
            else
            {
                String dialectId=dialect1.getId();
                List<Qwa>qwas1=new ArrayList<>();
                for( int i=0;i<qwas.size();i++) {
                    JSONObject jsonObject1 = qwas.getJSONObject(i);

                    Qwa qwa=new Qwa();
                    qwa.setDialectId(dialectId);
                    if(jsonObject1.containsKey("question"))
                    {
                        qwa.setQuestion(jsonObject1.getString("question"));
                    }
                    else
                    {
                        result.setMessage(jsonObject1+"问答对中在表中未找到问题");
                        return result;
                    }

                    if(jsonObject1.containsKey("answer"))
                    {
                        qwa.setAnswer(jsonObject1.getString("answer"));
                    }
                    else
                    {
                        result.setMessage(jsonObject1+"问答对中在表中未找到答案");
                        return result;
                    }
                    if(jsonObject1.containsKey("zan"))
                    {
                        qwa.setZan(Integer.valueOf(jsonObject1.getString("zan")));
                    }
                    else
                    {
                        result.setMessage(jsonObject1+"问答对中在表中未找到赞");
                        return result;
                    }
                    if(jsonObject1.containsKey("cai"))
                    {
                        qwa.setCai(Integer.valueOf(jsonObject1.getString("cai")));
                    }
                    else
                    {
                        result.setMessage(jsonObject1+"问答对中在表中未找到赞");
                        return result;
                    }
                    int q=qwaMapper.insert(qwa);
                    if(q!=1)
                    {
                        result.setMessage(qwa+"问答对未插入表中");
                        return result;
                    }
                }
            }
        }
        result.setMessage("完整对话记录已经完全插入");
        return  result;
    }

    public Map<String,List<Qwa>>updateMap(List<Qwa>qwas)
    {
        Map<String,List<Qwa>>map=new HashMap<>();
        for(Qwa qwa:qwas)
        {
            if(map.containsKey(qwa.getDialectId()))
            {
                List<Qwa>qwas1=map.get(qwa.getDialectId());
                qwas1.add(qwa);
                map.replace(qwa.getDialectId(),qwas1);
            }
            else
            {
                List<Qwa>qwas1=new ArrayList<>();
                qwas1.add(qwa);
                map.put(qwa.getDialectId(),qwas1);
            }
        }
        return map;
    }
    @Override
    public Result getAll(String username) {
        Result result=new Result<>();
        LambdaQueryWrapper<Dialect>dialectLambdaQueryWrapper=new LambdaQueryWrapper<>();
        dialectLambdaQueryWrapper.eq(Dialect::getUsername,username);
        List<Dialect>dialects=dialectMapper.selectList(dialectLambdaQueryWrapper);
        //这里一次性拿到所有的dialectId对应的问答
        LambdaQueryWrapper<Qwa>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(Qwa::getDialectId,"");
        List<Qwa>qwas=qwaMapper.selectList(queryWrapper);
        Map<String,List<Qwa>>qwaMap=updateMap(qwas);
        //这个map是用来更新每一个对话id对应的问答对内容
        if(dialects!=null&&dialects.size()!=0)
        {
            List<Map<String,Object>>resultList=new ArrayList<>();
            for(int i=0;i<dialects.size();i++)
            {
                //遍历每一个对话 取到对应的对话id去论文表里去取
                Map<String,Object>map=new HashMap<>();
                map.put("title",dialects.get(i).getTitle());
                map.put("username",dialects.get(i).getUsername());
                map.put("flagnum",dialects.get(i).getFlagnum());
                map.put("module",dialects.get(i).getModule());
                //todo 做一下优化 已完成
                map.put("data",qwaMap.get(dialects.get(i).getId()));
                resultList.add(map);
            }
            result.setResult(resultList);
            result.setMessage("已成功拿到数据");
            return result;
        }
        else
        {
            result.setResult(null);
            result.setMessage("该用户未有任何对话记录");
            return result;
        }
    }
    @Override
    public Result getAll1(String username,String module) {
        Result result=new Result<>();
        LambdaQueryWrapper<Dialect>dialectLambdaQueryWrapper=new LambdaQueryWrapper<>();
        dialectLambdaQueryWrapper.eq(Dialect::getUsername,username);
        dialectLambdaQueryWrapper.eq(Dialect::getModule,module);
        List<Dialect>dialects=dialectMapper.selectList(dialectLambdaQueryWrapper);
        if(dialects!=null&&dialects.size()!=0)
        {
            List<Map<String,Object>>resultList=new ArrayList<>();
            for(int i=0;i<dialects.size();i++)
            {
                Map<String,Object>map=new HashMap<>();
                map.put("title",dialects.get(i).getTitle());
                map.put("username",dialects.get(i).getUsername());
                map.put("flagnum",dialects.get(i).getFlagnum());
                map.put("module",dialects.get(i).getModule());
                LambdaQueryWrapper<Qwa>queryWrapper=new LambdaQueryWrapper<>();
                queryWrapper.eq(Qwa::getDialectId,dialects.get(i).getId());
                List<Qwa>qwas=qwaMapper.selectList(queryWrapper);
                map.put("data",qwas);
                resultList.add(map);
            }
            result.setResult(resultList);
            result.setMessage("已成功拿到数据");
            return result;
        }
        else
        {
            result.setResult(null);
            result.setMessage("该用户未有任何对话记录");
            return result;
        }
    }
}
